﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Publishing.WebControls;

namespace AsyncWebPart
{
    public class AsyncWebPartEditorPart : EditorPart
    {
        #region Private Members
        private TextBox _rowLimit;
        private CheckBox _usePaging;
        private TextBox _cacheMinutes;
        #endregion

        #region Constructor
        public AsyncWebPartEditorPart()
        {
            this.ID = "AsyncWebPartEditorPart1";
            this.Title = "Async Web Part";
        }
        #endregion


        protected override void CreateChildControls()
        {
            //Create the controls and set properties
            Panel mainPanel = new Panel();
            mainPanel.CssClass = "ms-TPBody";

            Panel subPanel = new Panel();

            _rowLimit = new TextBox();
            _rowLimit.ID = "txtRowLimit";

            _usePaging = new CheckBox();
            _usePaging.ID = "chkBoxUsePagingProperty";

            _cacheMinutes = new TextBox();
            _cacheMinutes.ID = "txtCacheMinutes";

            //Add the controls to the sub panel's control collection
            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionTitle\">Settings</div>"));

            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">Row Limit</div>"));
            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\">"));
            subPanel.Controls.Add(_rowLimit);
            subPanel.Controls.Add(new LiteralControl("</div></div>"));

            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">Use Paging?</div>"));
            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\">"));
            subPanel.Controls.Add(_usePaging);
            subPanel.Controls.Add(new LiteralControl("</div></div>"));

            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">Minutes To Cache Results</div>"));
            subPanel.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\">"));
            subPanel.Controls.Add(_cacheMinutes);
            subPanel.Controls.Add(new LiteralControl("</div></div>"));


            //Add the sub panel to the main panel
            mainPanel.Controls.Add(subPanel);

            //Add the main panel to the current object
            this.Controls.Add(mainPanel);
        }

        /// <summary>
        /// Apply changes to the webpart.
        /// </summary>
        /// <returns></returns>
        public override bool ApplyChanges()
        {
            //Ensure the controls are created before we reference them
            EnsureChildControls();

            //Set the properties of the web part
            if ((WebPartToEdit != null) && (WebPartToEdit is AsyncWebPart))
            {
                AsyncWebPart webPart = (AsyncWebPart)WebPartToEdit;
                webPart.RowLimit = _rowLimit.Text;
                webPart.UsePaging = _usePaging.Checked;

                int cacheMin;
                if (int.TryParse(_cacheMinutes.Text, out cacheMin))
                {
                    webPart.CacheResultMinutes = cacheMin;
                }
                else
                {
                    webPart.CacheResultMinutes = 0;
                }

                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// Set the editor with values from the webpart.
        /// </summary>
        public override void SyncChanges()
        {
            //Ensure the controls are created before we reference them
            EnsureChildControls();

            //Set the values of the editor
            if ((WebPartToEdit != null) && (WebPartToEdit is AsyncWebPart))
            {
                AsyncWebPart webPart = (AsyncWebPart)WebPartToEdit;

                _rowLimit.Text = webPart.RowLimit;
                _usePaging.Checked = webPart.UsePaging;
                _cacheMinutes.Text = webPart.CacheResultMinutes.ToString();
            }
        }

    }
}
